﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VoxelBlock;using Vector3Int = VoxelBlock.Vector3Int;

public class ReadColorHSVComponent : MonoBehaviour
{
    bool isRun = false;


    [SerializeField]
    Texture2D showPalatte;

    [SerializeField]
    Texture2D m_ColorBackPackR;
    [SerializeField]
    Texture2D m_ColorBackPackG;
    [SerializeField]
    Texture2D m_ColorBackPackB;

    [SerializeField]
    Camera selectCamera;

    [SerializeField]
    LayerMask layerMask;

    [SerializeField]
    Transform selectUVTransform;

    [SerializeField]
    Transform selectGameObject;


    [SerializeField]
    UIScrollBar scrollBarHSV;

    [SerializeField]
    UIInput inputR;
    [SerializeField]
    UIInput inputG;
    [SerializeField]
    UIInput inputB;


    [SerializeField]
    UIScrollBar sliderR;
    [SerializeField]
    UIScrollBar sliderG;
    [SerializeField]
    UIScrollBar sliderB;

    private int editingTexWidth;

    private int editingTexHeight;

    void Start()
    {
        editingTexWidth = showPalatte.width;
        editingTexHeight = showPalatte.height;
    }

    void Update () {
        if (isRun)
        {
            Vector3 cursorPos = Input.mousePosition;
            Ray ray = selectCamera.ScreenPointToRay(cursorPos);
            RaycastHit objhit;
            if (Physics.Raycast(ray, out objhit, 100, layerMask))
            {
                Debug.DrawLine(ray.origin, objhit.point, Color.red, 0.1f);
                Vector2 pixelUV = objhit.textureCoord;
                int pixelUVx = (int)(pixelUV.x * editingTexWidth);
                int pixelUVy = (int)(pixelUV.y * editingTexHeight);
                selectUVTransform.localPosition = new Vector3(pixelUV.x * editingTexWidth * 16, pixelUV.y * editingTexHeight * 16, 0);
                Color selectColor = showPalatte.GetPixel(pixelUVx, pixelUVy);
                isReturn_UpdateColor_RGB = true;
                sliderR.value = selectColor.r;
                sliderG.value = selectColor.g;
                sliderB.value = selectColor.b;

                Color32 color32 = selectColor;
                inputR.value = color32.r.ToString();
                inputG.value = color32.g.ToString();
                inputB.value = color32.b.ToString();

                float r, g, b;
                UpdateRGBColor(out r, out g, out b);

                isReturn_UpdateColor_RGB = false;


                VoxelEditorMastermind.instance.EditingTex.SetPixel(VoxelEditorMastermind.instance.nowSelectUV.x, VoxelEditorMastermind.instance.nowSelectUV.y, selectColor);
                VoxelEditorMastermind.instance.EditingTex.Apply();
            }
        }

        if (!isRun && Input.GetMouseButtonDown(0))
        {
            Vector3 cursorPos = Input.mousePosition;
            Ray ray = selectCamera.ScreenPointToRay(cursorPos);
            RaycastHit objhit;
            if (Physics.Raycast(ray, out objhit, 100, layerMask))
            {
                if (objhit.collider.transform == selectGameObject)
                {
                    isReturn_UpdateColor = false;
                    Debug.DrawLine(ray.origin, objhit.point, Color.red, 0.1f);
                    Vector2 pixelUV = objhit.textureCoord;
                    int pixelUVx = (int)(pixelUV.x * editingTexWidth);
                    int pixelUVy = (int)(pixelUV.y * editingTexHeight);
                    selectUVTransform.localPosition = new Vector3(pixelUVx * 16, pixelUVy * 16, 0);
                    isRun = true;
                }
            }
        }

        if (!Input.GetMouseButton(0))
        {
            isRun = false;
        }
    }




    bool isReturn_UpdateColor = true;
    public void UpdateColor()
    {
        if (isReturn_UpdateColor)
        {
            return;
        }
        UpdateHsvColor();

        int pixelUVx = (int)(selectUVTransform.localPosition.x / 16);
        int pixelUVy = (int)(selectUVTransform.localPosition.y / 16);
        isReturn_UpdateColor_RGB = true;
        Color selectColor = showPalatte.GetPixel(pixelUVx, pixelUVy);
        sliderR.value = selectColor.r;
        sliderG.value = selectColor.g;
        sliderB.value = selectColor.b;

        Color32 color32 = selectColor;
        inputR.value = color32.r.ToString();
        inputG.value = color32.g.ToString();
        inputB.value = color32.b.ToString();

        float r, g, b;
        UpdateRGBColor(out r, out g, out b);
        isReturn_UpdateColor_RGB = false;


        VoxelEditorMastermind.instance.EditingTex.SetPixel(VoxelEditorMastermind.instance.nowSelectUV.x, VoxelEditorMastermind.instance.nowSelectUV.y, selectColor);
        VoxelEditorMastermind.instance.EditingTex.Apply();
        showPalatte.Apply();

    }

    private void UpdateHsvColor()
    {
        float hsv = scrollBarHSV.value;
        for (float w = 0; w < showPalatte.width; w++)
        {
            for (float h = 0; h < showPalatte.height; h++)
            {
                Color curSelectedColor = Color.HSVToRGB(hsv, w / showPalatte.width, h / showPalatte.height);
                showPalatte.SetPixel((int)w, (int)h, curSelectedColor);
            }
        }
        showPalatte.Apply();
    }

    bool isReturn_UpdateColor_RGB = false;
    public void UpdateColor_RGB()
    {
        if (isReturn_UpdateColor_RGB)
        {
            return;
        }
        float r, g, b;
        UpdateRGBColor(out r, out g, out b);

        Color color = new Color(r, g, b, 1);
        Color32 color32 = color;
        inputR.value = color32.r.ToString();
        inputG.value = color32.g.ToString();
        inputB.value = color32.b.ToString();

        float h, s, v;
        Color.RGBToHSV(color, out h, out s, out v);
        scrollBarHSV.value = h;
        scrollBarHSV.value = h;
        selectUVTransform.localPosition = new Vector3(s * editingTexWidth * 16, v * editingTexHeight * 16, 0);

        UpdateHsvColor();
        VoxelEditorMastermind.instance.EditingTex.SetPixel(VoxelEditorMastermind.instance.nowSelectUV.x, VoxelEditorMastermind.instance.nowSelectUV.y, color);
        VoxelEditorMastermind.instance.EditingTex.Apply();


    }

    private void UpdateRGBColor(out float r, out float g, out float b)
    {
        r = sliderR.value;
        g = sliderG.value;
        b = sliderB.value;
        for (float w = 0; w < m_ColorBackPackR.width; w++)
        {
            Color curSelectedColor = new Color(w / m_ColorBackPackR.width, g, b);
            m_ColorBackPackR.SetPixel((int)w, 0, curSelectedColor);
            m_ColorBackPackR.SetPixel((int)w, 1, curSelectedColor);
        }
        m_ColorBackPackR.Apply();
        for (float w = 0; w < m_ColorBackPackR.width; w++)
        {
            Color curSelectedColor = new Color(r, w / m_ColorBackPackR.width, b);
            m_ColorBackPackG.SetPixel((int)w, 0, curSelectedColor);
            m_ColorBackPackG.SetPixel((int)w, 1, curSelectedColor);
        }
        m_ColorBackPackG.Apply();
        for (float w = 0; w < m_ColorBackPackR.width; w++)
        {
            Color curSelectedColor = new Color(r, g, w / m_ColorBackPackR.width);
            m_ColorBackPackB.SetPixel((int)w, 0, curSelectedColor);
            m_ColorBackPackB.SetPixel((int)w, 1, curSelectedColor);
        }
        m_ColorBackPackB.Apply();
    }


    private void OnEnable()
    {
        if (VoxelEditorMastermind.instance)
        {
            Vector2Int uv = VoxelEditorMastermind.instance.nowSelectUV;
            ShowColor(VoxelEditorMastermind.instance.EditingTex.GetPixel(uv.x, uv.y));

            EventDelegate.Add(sliderR.onChange, UpdateColor_RGB);
            EventDelegate.Add(sliderG.onChange, UpdateColor_RGB);
            EventDelegate.Add(sliderB.onChange, UpdateColor_RGB);
            EventDelegate.Add(scrollBarHSV.onChange, UpdateColor);

        }
    }

    public void ShowColor(Color color)
    {
        sliderR.value = color.r;
        sliderG.value = color.g;
        sliderB.value = color.b;
        float r, g, b;
        UpdateRGBColor(out r, out g, out b);

        float h, s, v;
        Color.RGBToHSV(color, out h, out s, out v);
        scrollBarHSV.value = h;
        scrollBarHSV.value = h;
        selectUVTransform.localPosition = new Vector3(s * editingTexWidth * 16, v * editingTexHeight * 16, 0);
        UpdateHsvColor();
    }

    public void SetColor()
    {
        
        if (isReturn_UpdateColor_RGB)
        {
            return;
        }
        byte byteValue;
        if (byte.TryParse(inputR.value, out byteValue))
        {
            sliderR.value = byteValue / 255f;
            inputR.value = byteValue.ToString();
        }
        if (byte.TryParse(inputG.value, out byteValue))
        {
            sliderG.value = byteValue / 255f;
            inputG.value = byteValue.ToString();
        }
        if (byte.TryParse(inputB.value, out byteValue))
        {
            sliderB.value = byteValue / 255f;
            inputB.value = byteValue.ToString();
        }

        float r, g, b;
        UpdateRGBColor(out r, out g, out b);

        float h, s, v;
        Color color = new Color(r, g, b, 1);
        Color.RGBToHSV(color, out h, out s, out v);
        scrollBarHSV.value = h;
        scrollBarHSV.value = h;
        selectUVTransform.localPosition = new Vector3(s * editingTexWidth * 16, v * editingTexHeight * 16, 0);
        UpdateHsvColor();
        VoxelEditorMastermind.instance.EditingTex.SetPixel(VoxelEditorMastermind.instance.nowSelectUV.x, VoxelEditorMastermind.instance.nowSelectUV.y, color);
        VoxelEditorMastermind.instance.EditingTex.Apply();
    }
}
